package com.aaa.member.service.impl;

import com.aaa.common.constants.ReturnCode;
import com.aaa.common.util.Result;
import com.aaa.member.dao.TbMemberDao;
import com.aaa.member.dao.tbAccountStatementDao;
import com.aaa.member.entity.TbMember;
import com.aaa.member.entity.tbAccountStatement;
import com.aaa.member.service.tbAccountStatementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * ( tbAccountStatement)表服务实现类
 *
 * @author makejava
 * @since 2020-11-18 08:54:32
 */
@Service(" tbAccountStatementService")
public class tbAccountStatementServiceImpl implements tbAccountStatementService {
    @Autowired(required = false)
    private tbAccountStatementDao tbAccountStatementDao;
    @Autowired(required = false)
    private TbMemberDao tbMemberDao;

    /**
     * 流水表中的金额 Amount充值到客户表的Account——Blance字段中
     * @param accountStatement
     * @return
     */
    @Transactional //开启事务
    @Override
    public Result addAccBlance(tbAccountStatement accountStatement) {
        //关联金额,原始的余额+流水表中的Amount金额值 得到账户余额
        if (accountStatement.getAmount() != 0){
            TbMember tbMember = new TbMember();
            tbAccountStatementDao.insertAmount (accountStatement);
            //根据客户id 执行查询流水表中的金额
            tbAccountStatement accountStatements = tbAccountStatementDao.selectById (accountStatement.getId ());
            //将流水表查询到的金额和客户表中的原始账户余额进行相加
            //得到流水表充值金额
            double amount = accountStatements.getAmount();
            //客户表的成长值 充值的金额等于成长值 得到成长值
            tbMember.setGrowth (amount);

            //账户余额为充值金额+原有的金额,不对
            //client.setAccountBalance (amount+client.getAccountBalance ());

            //关联客户Id
            tbMember.setId (accountStatement.getClientId ());
            //新增进去
            int i = tbMemberDao.insertAmountClient (tbMember);
            //从流水表中的一条数据，得到clientId   执行新增后获得id
            TbMember cli = tbMemberDao.queryById (tbMember.getId ());
            double accountBalance = cli.getAccountBalance ();

            //将余额添加到客户表的账户余额字段中,修改余额
            tbMember.setAccountBalance (amount+accountBalance);
            tbMemberDao.update (tbMember);//这样写其他数据都被修改掉了

            return new Result(ReturnCode.SUCCESSS.getCode ()
                    , ReturnCode.SUCCESSS.getMsg (), i);
        }
        return null;
    }


    @Override
    public Result queryAllByDynamic(tbAccountStatement tbAccountStatement) {
        //查询流水集合
        List<com.aaa.member.entity.tbAccountStatement>tbAccountStatements =tbAccountStatementDao.queryAll(tbAccountStatement);
        if (tbAccountStatements!=null&&tbAccountStatements.size()>0){
            return new Result(ReturnCode.SUCCESSS.getCode(), ReturnCode.SUCCESSS.getMsg(),tbAccountStatements);
        }
        return new Result(ReturnCode.SUCCESSS.getCode(), ReturnCode.SUCCESSS.getMsg(),null);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public tbAccountStatement queryById(Integer id) {
        return this.tbAccountStatementDao.queryById(id);
    }

    /**
     * 通过客户id查询相关明细信息

     * @return
     */
    @Override
    public Result selectByTbMemberId(Integer clientId) {

        return new Result(ReturnCode.SUCCESSS.getCode (), ReturnCode.SUCCESSS.getMsg (),
                tbAccountStatementDao.selectById (clientId));
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<tbAccountStatement> queryAllByLimit(int offset, int limit) {
        return this.tbAccountStatementDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param tbAccountStatement 实例对象
     * @return 实例对象
     */
    @Override
    public Result insert(tbAccountStatement tbAccountStatement) {
        int i = this.tbAccountStatementDao.insert(tbAccountStatement);
        if (i>0){
            return new Result(ReturnCode.SUCCESSS.getCode(), ReturnCode.SUCCESSS.getMsg(),null);
        }
        return new Result(ReturnCode.FAIL.getCode(), ReturnCode.FAIL.getMsg(),null);
    }

    /**
     * 修改数据
     *
     * @param tbAccountStatement 实例对象
     * @return 实例对象
     */
    @Override
    public tbAccountStatement update(tbAccountStatement tbAccountStatement) {
        this.tbAccountStatementDao.update(tbAccountStatement);
        return this.queryById(tbAccountStatement.getId());
    }

    @Override
    public List<tbAccountStatement> queryAll(tbAccountStatement accountStatement) {
        return this.tbAccountStatementDao.queryAll (accountStatement);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.tbAccountStatementDao.deleteById(id) > 0;
    }
}